package com.hanhan.chargingpile.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanhan.chargingpile.base.utils.JwtUtils;
import com.hanhan.chargingpile.user.pojo.entity.AppUser;
import com.hanhan.chargingpile.user.mapper.AppUserMapper;
import com.hanhan.chargingpile.user.pojo.vo.AppUserVO;
import com.hanhan.chargingpile.user.pojo.vo.LoginVO;
import com.hanhan.chargingpile.user.pojo.vo.RegisterVO;
import com.hanhan.chargingpile.user.pojo.vo.UserInfoVO;
import com.hanhan.chargingpile.user.service.AppUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanhan.common.exception.Assert;
import com.hanhan.common.result.ResponseEnum;
import com.hanhan.common.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author HanHan
 * @since 2023-03-23
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(RegisterVO registerVO) {
        AppUser appUser = new AppUser();
        appUser.setUserUid(registerVO.getTel());
        appUser.setTel(registerVO.getTel());
        appUser.setUserType(registerVO.getUserType());
        baseMapper.insert(appUser);
    }

    @Override
    public AppUserVO login(LoginVO loginVO, String ip) {
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", loginVO.getTel());
        AppUser appUser = baseMapper.selectOne(queryWrapper);
        if (appUser == null) {
            RegisterVO registerVO = new RegisterVO();
            registerVO.setTel(loginVO.getTel());
            registerVO.setUserUid(loginVO.getTel());
            registerVO.setUserType(loginVO.getUserType());
            register(registerVO);
            QueryWrapper<AppUser> registerQueryWrapper = new QueryWrapper<>();
            registerQueryWrapper.eq("tel", loginVO.getTel());
            AppUser registerAppUser = baseMapper.selectOne(registerQueryWrapper);
            return loginReturn(registerAppUser);
        }
        Assert.equals(appUser.getDisable() ? 1 : 0, AppUser.STATUS_NORMAL, ResponseEnum.LOGIN_LOKED_ERROR);

        return loginReturn(appUser);
    }

    @Override
    public boolean checkTel(String tel) {
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", tel);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }

    @Override
    public IPage<AppUser> listPage(Page<AppUser> pageParam, AppUser appUser) {
        return null;
    }

    @Override
    public void lock(Long id, Integer status) {

    }

    public AppUserVO loginReturn(AppUser appUser) {
        String token = JwtUtils.createToken(appUser.getId(), appUser.getNickname());
        AppUserVO appUserVO = new AppUserVO();
        appUserVO.setId(appUser.getId());
        appUserVO.setNickName(appUser.getNickname());
        appUserVO.setHeadUrl(appUser.getHeadUrl());
        appUserVO.setTel(appUser.getTel());
        appUserVO.setUserType(appUser.getUserType());
        appUserVO.setToken(token);
        return appUserVO;
    }
}
